<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="test"></div>
    <script src="../lib/react.development.js"></script>
    <!-- //引入核心库 -->
    <script src="../lib//react-dom.development.js"></script>
    <!-- //支持react的dom操作 -->
    <script src="../lib/babel.min.js"></script>
    <script type="text/babel">
        class MyComponent extends React.Component{
            constructor(){
                super()
                console.log('constructor')
            }
            state={opacity:1}
            demo(){
                console.log('demo')
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))//卸载dom的一个api
            }
            componentWillMount(){
                console.log('componentWillMount')
            }
            componentDidMount(){
                console.log('componentDidMount')
             this.timer=setInterval(()=>{
                let {opacity}=this.state
                opacity=opacity-0.1
                 if(opacity<=0){
                  opacity=1
                 }
            this.setState({opacity})
             },200)
            }
            componentWillUnmount (){
                clearInterval(this.timer)//卸载之前须清除定时器,不然会报错
            }
            render(){
                console.log('render')
                return(
                    <div>
                        <h3 ref={c=>this.h=c} style={{opacity:this.state.opacity}}>请关注我的变化</h3>
                        <button onClick={this.demo}>点我卸载dom</button>
                        </div>
                )
            }
        }
        ReactDOM.render(<MyComponent/>,document.getElementById('test'))
        </script>
</body>
</html>